#include "CuteVM.h"

#include "VMInstr.h"
#include "VMExec.h"

namespace CuteVM {

    bool Machine::nextPC() {
        if(code.hasPC(pc+1)) {
            pc++;
            return true;
        }
        return false;
    }

    //-------------- NOOP --------------//

    /* virtual */
    ExecResult & Noop::exec(Machine *vm) {
        if(vm->nextPC()) {
            return error(vm,"No next instruction");
        }
        return success(vm);
    }

    //-------------- PUSH --------------//

    /* virtual */
    QString Push::toString() {
        QString str = QString("PUSH ");
        str.append(value->toString());
        return str;
    }

    /* virtual */
    ExecResult & Push::exec(Machine *vm) {
        vm->push(value);
        if(vm->nextPC()) {
            return error(vm,"No next instruction");
        }
        return success(vm);
    }

    //-------------- POP --------------//

    /* virtual */
    QString Pop::toString() {
        QString str = QString("POP");
        return str;
    }

    /* virtual */
    ExecResult & Pop::exec(Machine *vm) {
        Value * val = vm->pop();
        if(vm->nextPC()) {
            return error(vm,"No next instruction");
        }
        return success(vm,val);
    }

} // namespace CuteVM
